package com.aaa.service.impl;

import com.aaa.entity.Comment;
import com.aaa.mapper.CommentMapper;
import com.aaa.mapper.VideoMapper;
import com.aaa.service.ICommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Bean
 * @since 2021-09-07
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    VideoMapper videoMapper;
    @Autowired
    NettyPushServerImpl nettyPushServer;

    public List<Map<String,Object>> selecomment(Integer vid,Integer uid){
        List<Map<String, Object>> seleone = videoMapper.seleone(vid, uid);
        List<Map<String,Object>> objects = new ArrayList<>();
        List<Map<String, Object>> selecomment = commentMapper.selecomment(vid,uid);
        for (Map map:selecomment){
            Integer id = (Integer) map.get("id");
            List<Map<String, Object>> list = commentMapper.reply_comment(vid, id,uid);
            map.put("low",list);
            objects.add(map);
        }
        seleone.get(0).put("comment",objects);
        return seleone;
    }

    public boolean addtop(Integer uid,String uname,Integer vid,String content){
        String seleuid = commentMapper.seleuid(vid);
        Integer addtop = commentMapper.addtop(uid, uname, vid, content,seleuid.equals(uid.toString())?null:0);
        if (addtop==1){
            if(!uid.toString().equals(seleuid)) {
                nettyPushServer.pushidServer(seleuid, uname + "评论了小主的作品哟~");
            }
            return true;
        }
        return false;
    }

    public boolean addtwe(Integer uid,String uname,Integer vid,Integer parentid,Integer parentuid,String content){
        Integer addtwo = commentMapper.addtwo(uid, uname, vid, parentid, content, uid == parentuid ? null : 0);
        if (addtwo==1){
            if(uid!=parentuid) {
                nettyPushServer.pushidServer(parentuid.toString(), uname + "回复了小主的评论哟~");
            }
            return  true;
        }
        return  false;
    }

    public boolean addtworeply(Comment comment,Integer parentuid){
        comment.setState(comment.getUid()==parentuid?null:0);
        Integer addtworeply = commentMapper.addtworeply(comment);
        if (addtworeply==1){
            if(comment.getUid()!=parentuid){
                nettyPushServer.pushidServer(parentuid.toString(), comment.getUname() + "回复了小主的评论哟~");
            }
            return true;
        }
        return  false;
    }


}
